#include "cubes.h"
#include "ui_cubes.h"
#include "QPainter"
#include "QTimer"
#include "QKeyEvent"

Cubes::Cubes(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Cubes),blsRun(false),blsOver(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,100,1100,1200));
    ui->pushButton->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_2->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_3->setFocusPolicy(Qt::NoFocus);
    ui->toolButton->setFocusPolicy(Qt::NoFocus);
}

Cubes::~Cubes()
{
    delete ui;
}
    void Cubes::paintEvent(QPaintEvent *event)
    {
        if(!blsRun)
        InitCubes();
        QPainter painters(this);
        //绘制背景
        painters.setPen(Qt::darkBlue);//画笔颜色
        painters.setBrush(Qt::black);//填充颜色
        painters.drawRect(10,10,1080,1180);
        painters.drawPixmap(10,10,1080,1180,QPixmap(":/images/pictures/picture.jpg"));
    //绘制格子
    painters.setPen(Qt::gray);//画笔颜色
    painters.setBrush(Qt::gray);//填充颜色
    painters.drawRect(15,15,570,1170);
    painters.setPen(Qt::black);
    painters.drawRect(595,15,490,600);
    painters.drawRect(595,600,490,550);
    painters.drawPixmap(15,15,570,1170,QPixmap(":/images/pictures/picture4.jpeg"));
    painters.drawPixmap(595,15,490,600,QPixmap(":/images/pictures/picture3.jpg"));
    painters.drawPixmap(595,600,490,570,QPixmap(":/images/pictures/picture3.jpg"));
    painters.setPen(Qt::gray);
    for(int i=1;i<48;i++)
    {
        painters.drawLine(25,i*25,575,i*25);
    }
    for(int i=1;i<24;i++)
    {
        painters.drawLine(i*25,25,i*25,1175);
    }
    //显示游戏开始和结束
    QFont font1("Microsoft YaHei",24);
    painters.setFont(font1);
    painters.setPen(Qt::white);
    painters.setBrush(Qt::white);
    if(blsOver)
    {
        painters.setPen(Qt::darkRed);
        painters.setBrush(Qt::darkRed);
        Display = "游戏结束";
    }
    if(vCube2.size()>0&&vCube1.size()==0)
    {
        painters.setPen(Qt::darkRed);
        painters.setBrush(Qt::darkRed);
        Display = "Win!!!";
        timer->stop();
    }
    painters.drawText(640,100,Display);
    //得分
    Score = "Score :";
    painters.drawText(640,200,Score);
    painters.drawText(840,205,QString::number(score));
    painters.drawText(640,280,"Next :");
    //绘制块块
    painters.setPen(Qt::black);
    painters.setBrush(Qt::black);
    painters.drawRect(700,300,300,200);
    painters.drawPixmap(700,300,300,200,QPixmap(":/images/pictures/picture3.jpg"));
    painters.setPen(Qt::gray);//画笔颜色
    painters.setBrush(Qt::cyan);//填充颜色
    painters.drawRect(CubeLeft);
    painters.drawRect(CubeRight);
    painters.drawRect(CubeTop);
    painters.drawRect(CubeBottom);
    painters.drawRects(&CubeRect[0],CubeRect.size());
    painters.drawPixmap(CubeLeft,QPixmap(":/images/pictures/picture1.jpg"));
    painters.drawPixmap(CubeBottom,QPixmap(":/images/pictures/picture1.jpg"));
    painters.drawPixmap(CubeTop,QPixmap(":/images/pictures/picture1.jpg"));
    painters.drawPixmap(CubeRight,QPixmap(":/images/pictures/picture1.jpg"));
    for(int i=0;i<4;i++)
        painters.drawPixmap(CubeRect[i],QPixmap(":/images/pictures/picture1.jpg"));

    painters.setPen(Qt::gray);
    painters.setBrush(Qt::darkGray);
    if(vCube.size()>0)
        painters.drawRects(&vCube[0],vCube.size());
    //painters.drawRects(&vCubeRect[0],vCubeRect.size());
    for(int i=0;i<vCube.size();i++)
        painters.drawPixmap(vCube[i],QPixmap(":/images/pictures/picture5.jpg"));
    for(int i=0;i<vCube1.size();i++)
        painters.drawPixmap(vCube1[i],QPixmap(":/images/pictures/picture16.jpg"));
    if(blsOver)
    {
        timer->stop();
    }
    if(paint)
    {
        update();
    }
    paint = false;
}
//块初始化
void Cubes::InitCubes()
{

    if(!blsRun)
    {
        if(!(vCube2.size()>0))
        {
            vCube2.resize(0);
            vCube.resize(0);
            vCube1.resize(0);
        }
        vCube.resize(vCube2.size());
        vCube1.resize(vCube2.size());
        for(int i = 0;i<vCube2.size();i++)
        {
            vCube[i] = vCube2[i];
            vCube1[i] = vCube2[i];
        }
        blsOver =false;
        Display = "请点击restart";
        score = 0;
    }
    init();
    Next();
    blsRun = true;
    if(blsRun)
        Display = "游戏开始";
    cDirection = 0;
    //QRect rect1(250,25,25,25),rect2(275,25,25,25),rect3(300,25,25,25),rect4(325,25,25,25);
    //vCubeRect1=rect1;vCubeRect2=rect2;vCubeRect3=rect3;vCubeRect4=rect4;


    timer = new QTimer(this);//设定计时器
    if(!blsOver&&blsRun)
    {
        if(50<600-score/100*h.speed-4*h.speed)
            times = 600-score/100*h.speed-4*h.speed;
        else
            times = 30;
        timer->start(times);//设定为500ms
    }
    if(!blsRun)
        Cube_update();
    connect(timer,SIGNAL(timeout()),SLOT(Cube_update()));
}


void Cubes::Cube_update()
{
    //CubeMain=vCubeRect.first();//主块
      /* for(int j=0;j<vCubeRect.size();j++)*/
      /* {*/
    if(blsRun&&!paint)
    {
           if(!(CubeLeft.left()==25&&cDirection==3)&&!(CubeRight.left()==550&&cDirection==4))//控制左右
           {
                directionLR1();
           }
           for(int i=0;i<vCube.size();i++)
           {
              if(CubeBottom==vCube[i]||CubeLeft==vCube[i]||CubeRight==vCube[i]||CubeTop==vCube[i])
              {
                  directionLR2();
                  break;
              }
           }
                 switch (cDirection)
                 {
                 case 1:
                    /* vCubeRect[vCubeRect.size()-1-j].setTop(vCubeRect[vCubeRect.size()-1-j].top()-25);
                     vCubeRect[vCubeRect.size()-1-j].setBottom(vCubeRect[vCubeRect.size()-1-j].bottom()-25);*/
                     Rotate();
                     break;
                 case 2:
                     if(times<100)
                        timer->start(10);
                     else
                         timer->start(100-score/50);
                     break;
                 }
                 if(CubeLeft.left()<25||CubeRight.left()>550)
                 {
                     if(cDirection==1)
                     {
                         if(cCube==2||cCube==5||cCube==6||cCube==9||cCube==10||cCube==11||cCube==13||cCube==18||cCube==14||cCube==15||cCube==16||cCube==17)
                         {
                             Rotate();
                             Rotate();
                         }
                         Rotate();
                     }
                 }
                 for(int i=0;i<vCube.size();i++)
                 {
                    if(CubeBottom==vCube[i]||CubeLeft==vCube[i]||CubeRight==vCube[i]||CubeTop==vCube[i]||CubeBottom.top()>=1150)
                    {
                        if(cDirection==1)
                        {
                            if(cCube==2||cCube==5||cCube==6||cCube==9||cCube==10||cCube==11||cCube==13||cCube==18||cCube==14||cCube==15||cCube==16||cCube==17)
                            {
                                Rotate();
                                Rotate();
                            }
                            Rotate();
                        }
                        break;
                     }
                  }
                 if(!(CubeBottom.top()==1150))
                 {
                    /* vCubeRect1[vCubeRect.size()-1-j].setTop(vCubeRect[vCubeRect.size()-1-j].top()+25);
                     vCubeRect1[vCubeRect.size()-1-j].setBottom(vCubeRect[vCubeRect.size()-1-j].bottom()+25);*/
                     CubeLeft = Down(CubeLeft);
                     CubeTop = Down(CubeTop);
                     CubeRight = Down(CubeRight);
                     CubeBottom = Down(CubeBottom);
                 }
                 else
                 {
                     vCube.resize(vCube.size()+4);
                     vCube[vCube.size()-1] = CubeBottom;
                     vCube[vCube.size()-2] = CubeLeft;
                     vCube[vCube.size()-3] = CubeTop;
                     vCube[vCube.size()-4] = CubeRight;
                     score+=10;
                     timer->stop();
                     Clear();
                     InitCubes();
                 }
                 for(int i=0;i<vCube.size();i++)
                 {

                    if(CubeBottom==vCube[i]||CubeLeft==vCube[i]||CubeRight==vCube[i]||CubeTop==vCube[i])
                    {
                        CubeLeft = Up(CubeLeft);
                        CubeTop = Up(CubeTop);
                        CubeRight = Up(CubeRight);
                        CubeBottom = Up(CubeBottom);
                        vCube.resize(vCube.size()+4);
                        vCube[vCube.size()-1] = CubeBottom;
                        vCube[vCube.size()-2] = CubeLeft;
                        vCube[vCube.size()-3] = CubeTop;
                        vCube[vCube.size()-4] = CubeRight;
                        score+=10;
                        for(int i=vCube.size()-1;i>vCube.size()-5;i--)
                        {
                            if(vCube[i].top()<=25)
                            {
                                blsOver = true;
                                Clear2();
                            }
                        }
                        timer->stop();
                        Clear();
                        InitCubes();
                        break;
                    }

                 }

    }
    update();//更新
    cDirection = 0;
}
void Cubes::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key = (QKeyEvent*) event;
    switch(key->key())
    {
        case Qt::Key_Up: cDirection = 1;
            break;
        case Qt::Key_Down: cDirection = 2;
            break;
        case Qt::Key_Left: cDirection = 3;
            break;
        case Qt::Key_Right: cDirection = 4;
            break;
    default:;
    }
}





